package com.guigu.community.controller;

import com.guigu.community.pojo.Carts;
import com.guigu.community.pojo.Products;
import com.guigu.community.service.ProductService;
import com.guigu.community.service.CartsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/carts")
@RestController
@CrossOrigin
public class CartsController {
    @Autowired
    private CartsService cartsservice;
    @Autowired
    private ProductService productService;
    //购物车添加
    @GetMapping("/add/{pid}/{uid}/{num}")
    @ResponseBody
    public String add(@PathVariable Integer pid, @PathVariable Integer uid,@PathVariable Integer num) {
        boolean save = cartsservice.addCarts(pid, uid,num);
        if (save)
            return "yes";
        return "no";
    }

    @PostMapping("listCarts/{uid}")
    @ResponseBody
    public List<Carts> listCarts(@PathVariable Integer uid) {
        List<Carts> carts1 = cartsservice.listCarts(uid);
        return carts1;
    }

    @PutMapping("updateNum/{cid}/{sum}")
    @ResponseBody
    public boolean updateNum(@PathVariable Integer cid,@PathVariable Integer sum){
        return cartsservice.updateCarts(cid, sum);
    }

    @DeleteMapping("delete/{cartId}")
    @ResponseBody
    public String delete(@PathVariable Integer cartId){
        boolean b = cartsservice.removeById(cartId);
        if (b)
          return "yes";
        return "no";
    }


}
